<?php
//debug($this->data);
//debug(json_encode($adjectivelist));  
?>

<?php
echo $javascript->link(array('autocomplete/jquery.tokeninput', 'tag-option/jquery.optionTree'));
echo $this->Html->css(array('autocomplete/token-input-facebook', 'autocomplete/token-input'));
?>
<style type="text/css">
    select.vertical {
        display: block;
    }
    
</style>
<script type="text/javascript">
    $(function() {


        var options = {
            empty_value: 'null',
            indexed: true,  // the data in tree is indexed by values (ids), not by labels
            on_each_change: 'tags', // this file will be called with 'id' parameter, JSON data must be returned
            
            loading_image: 'img/tag/ajax-load.gif',
            show_multiple: false, // if true - will set the size to show all options
            choose: '' // no choose item
        };

        var displayParents = function() {
            var labels = []; // initialize array
            $(this).siblings('select') // find all select
            .find(':selected') // and their current options
            .each(function() { labels.push($(this).text()); }); // and add option text to array
            // $("<div >").html( labels.join(' > ')+"<a class='delete' href='#'> X</a>").appendTo('#demo7-result');  // and display the labels
            if($(this).find(':selected').value!='0'){
                 $('#demo7-result').css("background-color","#BAF598");
                 $('#demo7-result').css("border","1px solid");
                $('#demo7-result').html($('<div class="tag_name">').text( labels.join(' > ')));  // and display the labels
            }
        }
  
        $.getJSON('tags?id=2', function(tree) { // initialize the tree by loading the file first
            $('input[id=demo7]').optionTree(tree, options).change(displayParents);
        
        
        });
        
        /*****************This event is called to add tags to the system. This will copy the content of
         * #demo7-result to #tagpool and also will append a hidden field with tagId of the selected tag.
         *  After the tag is added select will be reloaded  *******************************************/
       
        $('#more ').click(function(){
            //alert($('input[name^="data[Attribute]"]').length);
            //alert($('#demo7').length);
            if($("#demo7-result").is(':parent')){
                $('#tagpool').append('<div id="'+$('input[id=demo7]').val()+'" class="tag_container">'+$('#demo7-result').html()+"<div class='remove_tag'>X</div><div class='clearfix'></div><div class='edit_tag'>Edit</div><input type='hidden' name='tag_ids[]' value='"+$('input[id=demo7]').val()+"'/>");
                $('#tagpool').append($('#attributes').html());
                 $(".new_tag_container").removeClass("new_tag_container").addClass("tag_container");
                $("#attributes").empty();
                $("#demo7-result").empty();
                $('#demo7-result').css("background-color","");
                 $('#demo7-result').css("border","none");
                $.getJSON('tags?id=2', function(tree) { // initialize the tree by loading the file first
                    $('input[id=demo7]').optionTree(tree, options).change(displayParents);
        
        
                });
            }else{
                alert('Please Select one tag to add.');
            }
        });
         
             
                
        /*************** This will remove the tag**********************/       
             
        $(".remove_tag").live('click',function(){
            //alert('afa');
            //alert($($($(this).prev()).parent()).attr("id"));
            var currentID=$($(this).parent()).attr("id");
            //alert(currentID);
            $('#'+currentID).remove();
            
             
        });
        
        /********************************* Tag remove ends    *******************************/
       
       
        /*********************** Edit tag chain   **********************/
       
        $(".edit_tag").live('click',function(){
            //alert($($($(this).prev()).parent()).attr("id"));
            var currentID=$($(this).parent()).attr("id");
            //alert($('#'+currentID).remove().html());
            $('#demo7-result').append($('#'+currentID).remove().html());
            //$('#'+currentID).remove();
            var parent_ids='';
            
            $.ajax({
                url: "tag",
                global: false,
                type: "POST",
                data: {id : currentID },
                dataType: "html",
                async:false,
                success: function(response){
                    x=response;
                           
                }
            }
        );
            //alert(currentID);  
            var t=x.split(',');
            var options = {
                empty_value: 'null',
                indexed: true,  // the data in tree is indexed by values (ids), not by labels
                on_each_change: 'tags', // this file will be called with 'id' parameter, JSON data must be returned
                choose:'',
                preselect: {'data[Subject][tag_ids]':t}
            };

            var displayParents = function() {
                var labels = []; // initialize array
                $(this).siblings('select') // find all select
                .find(':selected') // and their current options
                .each(function() { labels.push($(this).text()); }); // and add option text to array
                $('<div>').text(this.value + ':' + labels.join(' > ')).appendTo('#demo5-result');  // and display the labels
            }

            $.getJSON('tags', function(tree) { // initialize the tree by loading the file first
                $('#demo7').optionTree(tree, options).change(displayParents);
                
            });
            
        });
        
        
        /*********************Edit Tag ends**********************/
   
    });


  
</script>

<script>
    $(document).ready(function(){
         
    });
</script>
<table id="table" width="900px" cellpadding="5" cellspacing="5">
    <tr align="center"><td><fieldset ><legend>Subject</legend>

                <?php
                echo $this->Form->Create('Subject', array('enctype' => 'multipart/form-data'));
                echo $this->Form->input('User.id', array('type' => 'hidden', 'value' => $this->data['Subject']['user_id']));
                echo $this->Form->input('Subject.id', array('type' => 'hidden', 'value' => $this->data['Subject']['id']));
                ?>
                <table  cellpadding="0" cellspacing="0" >




                    <tr>
                        <td id="label">Subject</td>
                        <td><?php echo $this->Form->input('Subject.subject', array('label' => '','id'=>'title')); ?><div id="error" style="color:red; font-size: 14px;margin: 3px 0 0 446px;"></div></td>
                        <script type="text/javascript">
                
                        $(document).ready(function() {
                              $("#title").tokenInput("<?php echo $this->base; ?>/admin/Subjects/synonyms",{
                                  theme: "facebook",
                                  preventDuplicates: true,
                                  queryParam: "search",
                                  searchDelay: 800,
                                  tokenLimit: 1,
                                  minChars: 3,
                            
                            prePopulate: <?php
                        if (isset($subjecttitle)) {
                            echo $subjecttitle;
                        }
                ?>
       });
                                    
                                    
   });
   $(document).ready(function(){
        var checkpool=$('#tagpool').html();
        if($.trim(checkpool)!=''){
            $(".tagwrapper").css("background","#EDFFED");
            $(".tagwrapper").css("border","1px solid #999");
        }else{
            $(".tagwrapper").css("background","");
            $(".tagwrapper").css("border","");
        }

    });
  
                </script>
                    </tr>                
                    <tr>
                        <td id="label" >Specifier</td>
                        <td><?php echo $this->Form->input('Subject.desc', array('label' => '', 'type' => 'text')); ?></td>
                    </tr>
                    <tr>
        <td id="label"  >Synonyms</td>
        <td><div id="redirect-div">

            </div>
            <?php
            //echo $this->Form->input('synonyms',array('label'=>'','size'=>'42'));
            echo $this->Form->input('redirects', array('label' => '', 'type' => 'textarea', 'cols' => '56', 'rows' => '10', 'id' => 'redirect', ''));
            ?>* In CSV(e.g Happy,Jolly) formate</td>
    </tr>
                    <tr><td>&nbsp;</td><td><div id="tagpool" class="tagwrapper">
                <?php
                $i = '0';
                if (isset($this->data['Subject']['taglist'])) {
                    foreach ($this->data['Subject']['taglist'] as $tag) {
                        if (!empty($tag['tag_id'])) {
                            echo "<div id=" . $tag['tag_id'] . " class='tag_container'><div class='tag_name'>" . $tag['tag'] . "</div><div class='remove_tag'>X</div><div class='clearfix'></div><div class='edit_tag'>Edit</div>
                                <input type='hidden' name='tag_ids[]' value='" . $tag['tag_id'] . "'/>
                                </div>";
                            if (isset($this->data['Attribute'])) {
                                foreach ($this->data['Attribute'] as $value) {
                                    if ($value['tag_id'] == $tag['tag_id']) {
                                        echo $this->Form->input('Attribute.' . $i . '.id', array('type' => 'hidden', 'value' => $value['id']));
                                        echo $this->Form->input('Attribute.' . $i . '.subject_id', array('type' => 'hidden', 'value' => $value['subject_id']));
                                        echo $this->Form->input('Attribute.' . $i . '.attribute', array('type' => 'hidden', 'value' => $value['attribute']));
                                        echo $this->Form->input('Attribute.' . $i . '.value', array('label' => $value['attribute'], 'value' => $value['value']));
                                        $i++;
                                    }
                                }
                            }
                        }
                    }
                }
                ?>
            </div>
                        </td>
                    </tr>
                    <tr>
        <td id="label" >Tag</td>
        <td style="margin-right: 20px;"><div><?php echo $this->Form->input('Subject.tag_ids', array('label' => '', 'id' => 'demo7', 'type' => 'hidden')); ?><span id="more" class="mybutton"><a href="#">Save</a></span><div id="error-message" class="error-message"></div><div id="tag_values"></div></div>
            <div class="results" id="demo7-result"></div><div id="hidden_val"></div>
            <div id="attributes"></div>
        </td>
    </tr>
                    <?php if (!empty($this->data['Subject']['image'])) { ?>
    <?php echo $this->Form->input('imagename', array('type' => 'hidden', 'value' => $this->data['Subject']['image'])); ?>
                        <tr style="vertical-align: top;">
                            <td id="label" >Image</td>
                            <td> <?php echo $this->Html->image('uploads/Subjects/' . $this->data['Subject']['image'], array('alt' => $this->data['Subject']['subject'], 'width' => '200', 'height' => '150')); ?>
                                <div style="color:#c73e14;"><?php echo $this->Html->link("Delete image?", array('controller' => 'Subjects', 'action' => 'delete_image', $this->data['Subject']['id']), array('style' => 'color:#c73e14;')); ?></div>
                            </td>
                        </tr>
<?php } else { ?>
                        <tr>
                            <td id="label">Image</td>
                            <td><?php echo $this->Form->input('Subject.image', array('label' => '', 'type' => 'file')); ?></td>
                        </tr>      
<?php } ?>
                    <tr>
        <td id="label">Adjectives</td>
        <td><div><?php
        echo $this->Form->input('Subject.old_adjectives',array('type'=>'hidden','value'=>$this->data['adjective_ids']));
        echo $this->Form->input('Subject.adjective_ids', array('label' => '', 'id' => 'demo-input-facebook-theme')); ?>   

                <script type="text/javascript">
                    $(document).ready(function() {
                        $("#demo-input-facebook-theme").tokenInput("<?php echo $this->base; ?>/admin/Subjects/adjectives", {
                            theme: "facebook",
                            preventDuplicates: true,
                            tokenLimit: 3,
                            queryParam: "q",
                            prePopulate: <?php
    if (isset($adjectivelist)) {
        echo json_encode($adjectivelist);
    } else {
        echo "[]";
    }
    ?>
                            
            });
        });
                </script>
            </div>
        </td>
    </tr>  
                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td id="label">17+ &nbsp;&nbsp;&nbsp;?</td>
                        <td><?php echo $this->Form->input('17+', array('label' => '', 'type' => 'checkbox')); ?></td>
                    </tr>

                    <tr style="height:80px">
                        <td id="label" style="vertical-align:middle;">Rating</td>
                        <td><span class="rating"><?php if(isset($this->data['SubjectRate']['0']['rating'])){ echo $this->data['SubjectRate']['0']['rating'];}else{
                            echo "0.0";
                        } ?></span></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div style="padding:5px;" width="100%">
                                <div style="float:left;width:50%;">
                                    <span id="label">Added By:</span>
                                    <span style="margin: 0 5px 0px 10px;"><?php echo ucfirst($this->data['User']['username']); ?></span>
                                </div>
                                <div style="float:right;width:50%;" >
                                    <span id="label">Added On:</span>
                                    <span style="padding-left: 10px;"><?php $timestamp=strtotime($this->data['Subject']['created']); echo date("Y-m-d",$timestamp); ?></span>
                                </div>
                            </td>
                            </tr>



                    <tr><td colspan="2">&nbsp;</td></tr>


                </table>
            </fieldset>
        </td></tr>
    <tr>
        <td>
            <table width="100%">
                <tr>
                    <td align="center"><fieldset ><legend>Give Credits for</legend>
                            <table>
                                <?php
                                $i = 0;
                                foreach ($credits as $credit) {
                                    if ($i % 2 == 0) {
                                        echo "<tr>";
                                    }
                                    echo "<td>" . $credit['Credit']['title'] . "</td><td>";
                                    echo $this->Form->input($credit['Credit']['id'], array('type' => 'checkbox', 'label' => '', 'title' => 'Approve'));
                                    echo "</td>";
                                    $i++;
                                }
                                ?>   
                            </table>
                        </fieldset>
                    </td>
                </tr> 
            </table>
<?php echo $this->Form->end('Approve'); ?>
        </td></tr>
</table>